package com.sqj.proxyPattern.dynamicProxy;

import com.sqj.proxyPattern.IOrderService;
import com.sqj.proxyPattern.Order;
import com.sqj.proxyPattern.OrderServiceImpl;
import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.util.Date;

public class DynamicProxyTest {
    public static void main(String[] args) {
        Order order=new Order();
        order.setCreateDatetime(new Date());
        IOrderService orderService = (IOrderService) new OrderServiceDynamicProxy().getInstance(new OrderServiceImpl());
        orderService.createOrder(order);

        //通过反编译工具可以查看源代码
        byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{IOrderService.class});
        try {
            FileOutputStream fos = new FileOutputStream("$Proxy0.class");
            fos.write(bytes);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
